﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfSnqkGasAnalysis
{
    /// <summary>
    /// PageSuHyCalc.xaml 的交互逻辑
    /// </summary>
    public partial class PageSuHyCalc : Page
    {
        public PageSuHyCalc()
        {
            InitializeComponent();
        }

        //计算
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //VO-V平均：avg_cha_value
            double v0_avg = 0;
            double.TryParse(avg_cha_value.Text, out v0_avg);

            //M，硫代硫酸钠
            double M;
            double.TryParse(suNaDensity.Text, out M);

            //Pv，根据表格算来，对照温度值
            double pv = 0;

            //t，取样温度
            double temp = 0;
            double.TryParse(quYangWenDu.Text, out temp);

            if (radioZhen.IsChecked.Value)
            {
                pv = 0;
            }
            else
            {
                //根据输入的温度取值
                switch (temp)
                {
                    case 0: pv = 0.6; break;
                    case 1:
                    case 2: pv = 0.7; break;

                    case 3:
                    case 4:
                        pv = 0.8; break;

                    case 5:
                    case 6:
                        pv = 0.9; break;

                    case 7:
                        pv = 1; break;

                    case 8:
                    case 9:
                        pv = 1.1; break;
                    case 10:
                        pv = 1.2; break;
                    case 11:
                        pv = 1.3; break;
                    case 12:
                        pv = 1.4; break;
                    case 13:
                        pv = 1.5; break;
                    case 14:
                        pv = 1.6; break;
                    case 15:
                        pv = 1.7; break;
                    case 16:
                        pv = 1.8; break;
                    case 17:
                        pv = 1.9; break;
                    case 18:
                        pv = 2.1; break;
                    case 19:
                        pv = 2.2; break;
                    case 20:
                        pv = 2.3; break;
                    case 21:
                        pv = 2.5; break;
                    case 22:
                        pv = 2.6; break;
                    case 23:
                        pv = 2.8; break;
                    case 24:
                        pv = 3; break;
                    case 25:
                        pv = 3.2; break;
                    case 26:
                        pv = 3.4; break;
                    case 27:
                        pv = 3.6; break;
                    case 28:
                        pv = 3.8; break;
                    case 29:
                        pv = 4; break;
                    case 30:
                        pv = 4.2; break;
                    case 31:
                        pv = 4.5; break;
                    case 32:
                        pv = 4.7; break;
                    case 33:
                        pv = 5.0; break;
                    case 34:
                        pv = 5.3; break;
                    case 35:
                        pv = 5.6; break;
                    case 36:
                        pv = 5.9; break;
                    case 37:
                        pv = 6.3; break;
                    case 38:
                        pv = 6.6; break;
                    case 39:
                        pv = 7; break;
                    case 40:
                        pv = 7.4; break;
                    case 41:
                        pv = 7.8; break;
                    case 42:
                        pv = 8.2; break;
                    case 43:
                        pv = 8.6; break;
                    case 44:
                        pv = 9.1; break;
                    case 45:
                        pv = 9.6; break;
                    case 46:
                        pv = 10.1; break;
                    case 47:
                        pv = 10.6; break;
                    case 48:
                        pv = 11.2; break;
                    case 49:
                        pv = 11.8; break;

                    default:
                        pv = 11.8; break;
                }
            }
            //大气压力
            double p = 0;
            double.TryParse(daQiYaLi.Text, out p);

            //取样体积
            double sampleVolume = 0;
            double.TryParse(quYangTiJi.Text, out sampleVolume);

            //计算标准体积
            double bztj = (p - pv) * sampleVolume * 293.2 / ((273.2 + temp) * 101.3);
            double bztjF3 = Convert.ToDouble(bztj.ToString("F0"));
            //double bztjF3 = bztj;

            //标准体积
            normalVolume.Text = bztjF3.ToString("F0");

            //硫化氢H2S含量mg/m3计算
            double h2sMg = v0_avg * M * 17.04 * 1000 * 1000 / bztjF3;
            sulyContent.Text = h2sMg.ToString("F0");

            //H2S百分含量
            double h2sPercent = v0_avg * M * 11.88 * 100 / bztjF3;
            percentContent.Text = h2sPercent.ToString("F3");
        }

        //刷新
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.suNaDensity.Text = "";

            this.daQiYaLi.Text = "";
            this.quYangWenDu.Text = "";
            this.quYangTiJi.Text = "";
            this.avg_cha_value.Text = "";

            this.normalVolume.Text = "";
            this.sulyContent.Text = "";
            this.percentContent.Text = "";
        }


        private void quYangWenDu_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            var textElem = (sender as TextBox);

            //解决连续输入很多.
            if ((
                textElem.Text.StartsWith(".") || string.IsNullOrWhiteSpace(textElem.Text)
                || textElem.Text.Contains(".")
                ) && e.Text == ".")
            {
                e.Handled = true;
                return;
            }

            //禁止在0前面继续输入0
            int index = ((System.Windows.Controls.TextBox)e.Source).CaretIndex;
            if (textElem.Text.StartsWith("0.") && index == 0 && e.Text == "0")
            {
                e.Handled = true;
                return;
            }

            //不能输入非数字
            if (Regex.IsMatch(e.Text, @"[^\d+\.{0,1}\d+]+"))
            {
                e.Handled = true;
                return;
            }
        }

        private void quYangWenDu_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            TextBox textBox = sender as TextBox;
            if (!string.IsNullOrWhiteSpace(textBox.Text))
            {
                if (textBox.Text == "0.0" || textBox.Text.StartsWith("00"))
                {
                    textBox.Text = textBox.Text.Replace("00", "");
                }
                if (!textBox.Text.StartsWith("0.") && Regex.IsMatch(textBox.Text, @"0+[1-9]+\.*\d+", RegexOptions.IgnoreCase))
                {
                    textBox.Text = textBox.Text.TrimStart('0');
                }

                //输入的不是小数或正整数           
                Regex regex = new Regex(@"[^\d+\.{0,1}\d+]", RegexOptions.IgnoreCase);
                textBox.Text = regex.Replace(textBox.Text, "");
            }
        }

        //禁用右键粘贴字符串。//创建时间：2022-12-5 14:56:01
        private void quYangWenDu_PreviewMouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
        }
    }
}
